import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/28 9:04
 * @description 栈
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.scoreOfParentheses("(()(()))");
    }

    public int scoreOfParentheses(String s) {
        Stack<Integer> stack = new Stack();
        // The score of the current frame
        stack.push(0);

        for (char c: s.toCharArray()) {
            if (c == '(') {
                stack.push(0);
            } else {
                int v = stack.pop();
                int w = stack.pop();
                stack.push(w + Math.max(2 * v, 1));
            }
        }

        return stack.pop();
    }

}
